#ifndef __SOCKET_HPP__
#define __SOCKET_HPP__

#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory>
#include "Logger.hpp"
#include "InetAddr.hpp"

static const int gsockfd = -1;
static const int gbacklog = 8;

enum
{
    OK,
    CREATE_ERR,
    BIND_ERR,
    LISTEN_ERR
};

class Socket
{
public:
    virtual ~Socket(){}
    virtual void CreateSocketOrDie() = 0;
    virtual void BindSocketOrDie(int port) = 0;
    virtual void ListenSocketOrDie(int backlog) = 0;
    virtual std::shared_ptr<Socket> Accept(InetAddr* clientaddr) = 0;//目前我觉得unique_ptr更好，但是好像还有连接池这个玩意
    virtual ssize_t Recv(std::string* out) = 0;
    virtual ssize_t Send(const std::string& in) = 0;
    virtual bool Connect(InetAddr& peer) = 0;
    virtual int SockFd() = 0;
    virtual void Close() = 0;
public:
    void BuildListenSocketMethod(int _port)
    {
        CreateSocketOrDie();
        BindSocketOrDie(_port);
        ListenSocketOrDie(gbacklog);
    }
    void BuildClientSocketMethod()
    {
        CreateSocketOrDie();
    }

};


class TcpSocket : public Socket
{
public:
    TcpSocket()
        :_sockfd(gsockfd)
    {}
    TcpSocket(int fd)
        :_sockfd(fd)
    {}

    void CreateSocketOrDie() override
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(_sockfd < 0)
        {
            LOG(Loglevel::FATAL) << "create socket error!";
            exit(CREATE_ERR);
        }
        LOG(Loglevel::INFO) << "create socket success!";
    }

    void BindSocketOrDie(int port) override
    {
        InetAddr local(port);
        int n = bind(_sockfd, local.Addr(), local.Length());
        if(n != 0)
        {
            LOG(Loglevel::FATAL) << "bind socket error!";
            exit(BIND_ERR);
        }
        LOG(Loglevel::INFO) << "bind socket success!";
    }

    void ListenSocketOrDie(int backlog) override
    {
        int n = listen(_sockfd, backlog);
        if(n != 0)
        {
            LOG(Loglevel::FATAL) << "listen socket error!";
            exit(LISTEN_ERR);
        }
        LOG(Loglevel::INFO) << "listen socket success!";
    }

    std::shared_ptr<Socket> Accept(InetAddr* clientaddr) override
    {
        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        int fd = accept(_sockfd, (struct sockaddr*)&peer, &len);
        if(fd < 0)
        {
            LOG(Loglevel::WARNING) << "accept socket error!";
            return nullptr;
        }
        LOG(Loglevel::INFO) << "accept socket success!";
        clientaddr->Init(peer);
        return std::make_shared<TcpSocket>(fd);
    }

    ssize_t Recv(std::string* out) override//调用Recv和Send函数时sockfd肯定已经变成了用来传输消息的fd
    {
        char buffer[1024];
        ssize_t n = recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
        if(n > 0)
        {
            buffer[n] = 0;
            *out += buffer;
        }
        return n;
    }

    ssize_t Send(const std::string& in) override
    {
        return send(_sockfd, in.c_str(), in.size(), 0);
    }

    bool Connect(InetAddr& peer) override
    {
        int n = connect(_sockfd, peer.Addr(), peer.Length());
        if(n >= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    int SockFd() override
    {
        return _sockfd;
    }

    void Close() override
    {
        if(_sockfd >= 0)
            close(_sockfd);
    }


    ~TcpSocket(){};
private:
    int _sockfd;
};



#endif